package com.mdc.mansion.core.modules.restaurant.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.util.I18nUtil;
import com.mdc.mansion.common.util.MD5Utils;
import com.mdc.mansion.core.modules.restaurant.AccountService;
import com.mdc.mansion.core.modules.restaurant.RestaurantStaffService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.RestaurantStaff;

/**<p>com.mdc.mansion.core.modules.restaurant.impl.AccountServiceImpl</p>
 * <p>Title: AccountServiceImpl</p>
 * <p>Copyright: Copyright © 2010-2014</p>
 * <p>Company: jumei</p>
 * @author xiang.liu
 * @version 1.0
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Resource
    private RestaurantStaffService restaurantStaffService;
    
    @Transactional
    @Override
    public void register(String loginAccount, String password, String tel,String countryCode) {
        RestaurantStaff restaurantStaff = restaurantStaffService.getByLoginAccount(loginAccount);
        if(restaurantStaff != null){
            throw new BusinessException(I18nUtil.getMessage("account.has.exist",loginAccount));// 账号已存在
        }
        // 写入数据
        String passwordMd5 = MD5Utils.string2MD5(password);
        restaurantStaff = new RestaurantStaff();
        restaurantStaff.setLoginAccount(loginAccount);
        restaurantStaff.setPassword(passwordMd5);
        restaurantStaff.setCountryCode(countryCode);
        restaurantStaff.setRoleType("ADMIN");
        restaurantStaff.setTel(tel);
        // 写入表
        restaurantStaffService.save(restaurantStaff);
    }
    @Override
    public RestaurantStaff login(String loginAccount, String password) {
        RestaurantStaff restaurantStaff = restaurantStaffService.getByLoginAccount(loginAccount);
        if(restaurantStaff == null){
           // throw new BusinessException("账号"+loginAccount+"不存在");
              throw new BusinessException(I18nUtil.getMessage("account.notexist",loginAccount));// 账户不存在
        }
        // 验证密码是否正确
        String passwordMd5 = MD5Utils.string2MD5(password);
        if(!passwordMd5.equals(restaurantStaff.getPassword())){
            throw new BusinessException(I18nUtil.getMessage("login.password.error"));// 密码错误
        }
        return restaurantStaff;
    }
    @Transactional
    @Override
    public void changePassword(String oldPassword, String newPassword) {
        String loginAccount = ContextHolder.currentLoginAccount();
        RestaurantStaff restaurantStaff = restaurantStaffService.getByLoginAccount(loginAccount);
        if(restaurantStaff == null){
            throw new BusinessException(I18nUtil.getMessage("login.data.exception"));// 登录信息异常
        }
        // 验证旧密码是否一致
        String oldPasswordMd5 = MD5Utils.string2MD5(oldPassword);
        if(!oldPasswordMd5.equals(restaurantStaff.getPassword())){
            throw new BusinessException(I18nUtil.getMessage("old.password.not.incorrect"));
        }
        String newPasswordMd5 = MD5Utils.string2MD5(newPassword);
        restaurantStaff.setPassword(newPasswordMd5);
        restaurantStaffService.updateSelective(restaurantStaff);
    }
    @Transactional
    @Override
    public void forgetResetPassword(String loginAccount, String password) {
        RestaurantStaff restaurantStaff = restaurantStaffService.getByLoginAccount(loginAccount);
        String passwordMd5 = MD5Utils.string2MD5(password);
        restaurantStaff.setPassword(passwordMd5);
        restaurantStaffService.updateSelective(restaurantStaff);
    }
    
    
}
